package com.enthusiasm.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;

import com.enthusiasm.constants.AbstractSurfaceView;
import com.enthusiasm.constants.BitmapManager;
import com.enthusiasm.constants.SoundManager;
import com.enthusiasm.engine.MyButton;
import com.enthusiasm.engine.MyRect;
import com.enthusiasm.frame.GameMenuActivity;
import com.enthusiasm.frame.GameModeActivity;
import com.spx.LGraphics;
import com.spx.SpriteX2;
import com.spx.SpriteXData;
/**
 * 
 *
 */
public class  GameMenuView extends AbstractSurfaceView{
	private String tag = "GameMenuView"; 
	
	private MyRect screenRect = null;
	private Rect sunRect = null;
	private MyRect titleRect = null;
	private MyRect startRect = null;
	private MyRect mediaRect = null;
	private MyRect soundRect = null;
	private MyRect treeRect = null;
	private MyRect grassRect = null;
	private MyRect wolfRect = null;
	private MyRect puzzleRect = null;
	private MyButton startGame; //game start Button
	private MyButton media;// background media Button
	private MyButton sound;//soundPool Button
	private Bitmap bmpGameStart_before;
	private Bitmap bmpGameStart_after;
	private Bitmap bmpMedia_open;
	private Bitmap bmpMedia_close;
	private Bitmap bmpSound_open;
	private Bitmap bmpSound_close;
	private Bitmap bmpGameTitle;
	private Bitmap bmpBackground;
	private Bitmap bmpTree;
	private Bitmap bmpGrass;
	private Bitmap bmpWolf01;
	private Bitmap bmpWolf02;
	private Bitmap bmpPuzzle;
	
	
	//动画变量
	private SpriteXData wolf_male = null;
	private SpriteXData sheep_male = null;
	private LGraphics Lgraph = null;
	private SpriteX2 sprite_wolf_male = null;
	private SpriteX2 sprite_sheep = null;
	private int wolf_male_x;
	private int sheep_x;
	
	private Bitmap bmpSun;
	
	
	public GameMenuView(Context context) {
		super(context);
	}

	private void initSprite() {
		if(wolf_male == null){
			wolf_male=SpriteXData.loadSpriteX("/wolf_male.sprite");
			wolf_male.loadImage("/wolf_male.png");
			sprite_wolf_male=new SpriteX2(wolf_male);
			wolf_male_x =0;
			int wolf_male_y = screenH*7/8;
			sprite_wolf_male.setPosition(wolf_male_x,wolf_male_y);
			sprite_wolf_male.setActionFromIndex(0);
			sprite_wolf_male.setDelay(100);
			sprite_wolf_male.setPlayCount(-1);
			sprite_wolf_male.play();
		}
		if(sheep_male == null){
			sheep_male=SpriteXData.loadSpriteX("/sheep.sprite");
			sheep_male.loadImage("/sheep.png");
			sprite_sheep=new SpriteX2(sheep_male);
			sheep_x =screenW/4;
			int sheep_y = screenH*8/9 ;
			sprite_sheep.setPosition(sheep_x,sheep_y);
			sprite_sheep.setActionFromIndex(0);
			sprite_sheep.setDelay(100);
			sprite_sheep.setPlayCount(-1);
			sprite_sheep.play();
		}
	}

	public void ActionDraw(Canvas canvas) {
		if(Lgraph==null)
			Lgraph=new LGraphics(canvas,canvas.getWidth(),canvas.getHeight());
		else{
			Lgraph.setGraphics(canvas,canvas.getWidth(),canvas.getHeight());
		}
		Lgraph.creal();
		sprite_wolf_male.paintLayer(Lgraph);
		sprite_sheep.paintLayer(Lgraph);
	}
	
	@Override
	public void draw() {
		canvas.drawBitmap(bmpBackground, null, screenRect, paint);
		drawSunshine();
		canvas.drawBitmap(bmpGameTitle, null,titleRect, paint);
		canvas.drawBitmap(bmpPuzzle,null,puzzleRect,paint);
		canvas.drawBitmap(bmpTree, null,treeRect, paint);
		startGame.draw(canvas, paint);
		drawWolf();
		if(isMediaStop)
			canvas.drawBitmap(bmpMedia_close, null,mediaRect, paint);
		else
			canvas.drawBitmap(bmpMedia_open, null,mediaRect, paint);
		if(isSoundStop)
			canvas.drawBitmap(bmpSound_close, null,soundRect, paint);
		else
			canvas.drawBitmap(bmpSound_open, null,soundRect, paint);
		ActionDraw(canvas);
		canvas.drawBitmap(bmpGrass,null,grassRect , paint);
	}

	/*红太狼*/
	private void drawWolf() {
		if(time % 6 <3){
			canvas.drawBitmap(bmpWolf01, null,wolfRect, paint);
		}else{
			canvas.drawBitmap(bmpWolf02, null, wolfRect, paint);
		}
	}

	/*太阳光*/
	private void drawSunshine() {
		canvas.save();
		canvas.rotate(degrees);
		canvas.drawBitmap(bmpSun, null,screenRect, paint);
		canvas.restore();
	}

	private int time = 0;
	private int top = 0;
	private int sun = 0;
	private int degrees = - 10;
	@Override
	public void logic() {
		/*狼追羊*/
		sprite_wolf_male.update();
		sprite_sheep.update();
		sheep_x = sheep_x + 5;
		wolf_male_x = wolf_male_x + 5;
		sprite_wolf_male.setX(wolf_male_x);
		sprite_sheep.setX(sheep_x);
		
		if(wolf_male_x > screenW+50){
			wolf_male_x = -screenW/4;
			sheep_x = 0;
		}
		
		/*字体动画*/
		time ++;
		if(time%20 == 0)
			top++;
		
		if(top%2 == 0 ){
			titleRect.top++;
		}else
			titleRect.top --;
		/*光线动画*/
		if(time % 30 == 0)
			sun ++;
		if(sun%2 == 0){
			degrees ++;
		}else
			degrees --;
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		startGame.onTouchEvent(event);
		media.onTouchEvent(event);
		sound.onTouchEvent(event);
		return true;
	}

	@Override
	public void initBitmap() {
		Log.v(tag, "a: initBitmap()");
		BitmapManager.getInstance(context).initMenuBitmap();
		bmpPuzzle = BitmapManager.getInstance(context).bmpPuzzle;
		bmpWolf01 = BitmapManager.getInstance(context).bmpWolf01;
		bmpWolf02 = BitmapManager.getInstance(context).bmpWolf02;
		bmpSun = BitmapManager.getInstance(context).bmpSun;
		bmpTree = BitmapManager.getInstance(context).bmpTree;
		bmpGrass = BitmapManager.getInstance(context).bmpGrass;
		bmpBackground = BitmapManager.getInstance(context).bmpBackground;
		bmpGameTitle = BitmapManager.getInstance(context).bmpGameTitle;
		bmpGameStart_before = BitmapManager.getInstance(context).bmpGameStart_before;
		bmpGameStart_after = BitmapManager.getInstance(context).bmpGameStart_after;
		bmpMedia_open = BitmapManager.getInstance(context).bmpMedia_open;
		bmpMedia_close = BitmapManager.getInstance(context).bmpMeida_close;
		bmpSound_open = BitmapManager.getInstance(context).bmpSound_open;
		bmpSound_close = BitmapManager.getInstance(context).bmpSound_close;
		startGame = new MyButton(bmpGameStart_before,bmpGameStart_after) {
			@Override
			public void doClick() {
				SoundManager.getInstance(context).playSoundClick();
				Intent intent = new Intent();
				intent.setClass(context, GameModeActivity.class);
				context.startActivity(intent);
				((GameMenuActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
				((GameMenuActivity)context).finish();
			}
		};
		media = new MyButton(bmpMedia_open,bmpMedia_close) {
			@Override
			public void doClick() {
				SoundManager.getInstance(context).playSoundClick();
				if(isMediaStop == false){
					isMediaStop = true;
					SoundManager.getInstance(context).pauseBackgroundSound();
					SoundManager.getInstance(context).setIsMediaStop(isMediaStop);
				}else{
					isMediaStop = false;
					SoundManager.getInstance(context).resumeBackGroundSound();
					SoundManager.getInstance(context).setIsMediaStop(isMediaStop);
				}
			}
		};
		sound = new MyButton(bmpSound_open,bmpSound_close) {
			@Override
			public void doClick() {
				if(isSoundStop == false){
					isSoundStop = true;
					SoundManager.getInstance(context).releaseSoundPool();
					SoundManager.getInstance(context).setIsSoundStop(isSoundStop);
				}else{
					isSoundStop = false;
					SoundManager.getInstance(context).initSoundPool();
					SoundManager.getInstance(context).setIsSoundStop(isSoundStop);
				}
			}
		};
	}


	@Override
	public void initRect() {
		if(sunRect == null)
			sunRect = new Rect(-screenW/10, screenH/10, screenW, screenH);
		if(screenRect == null)
			screenRect = new MyRect(0, 0, screenW,screenH);
		if(puzzleRect == null)
			puzzleRect = new MyRect(screenW*2/3, screenH/4, screenW/5, screenH/7);
		if(wolfRect == null)
			wolfRect = new MyRect(screenW*3/8-screenW/40,screenH*7/16-screenH/10,screenW/8,screenH/4);
		if(treeRect == null)
			treeRect = new MyRect(screenW*2/3, screenH/2-screenH/20, screenW/4, screenH/2+screenH/40);
		if(grassRect == null)
			grassRect = new MyRect(0, screenH-screenH/8, screenW, screenH/8);
		if(titleRect == null)
			titleRect = new MyRect(screenW*7/24, screenH/10, screenW*5/12, screenH/6);
		if(startRect == null){
			startRect = new MyRect(screenW*3/8,screenH*7/16,screenW/4,screenH/8);
			startGame.setRect(startRect);
		}
		if(mediaRect == null){
			mediaRect = new MyRect(screenW*14/16,
					screenH/12,
					screenW/20,
					screenH*10/100);
			media.setRect(mediaRect);
		}
		if(soundRect == null){
			soundRect = new MyRect(screenW*15/16,
					screenH/12,
					screenW/20,
					screenH*10/100);
			sound.setRect(soundRect);
		}
		initSprite();
	}


	@Override
	public void releaseBitmap() {
		Log.v(tag, "a: releaseBitmap()");
		BitmapManager.getInstance(context).releaseMenuBitmap();
	}
}